﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI
{
    public class LockButton : Button
    {
        private enum ButtonState { None = 0, Painting = 1, Ready = 2 };

        private ButtonState bState = ButtonState.None;

        private readonly Timer timer = new Timer();

        private int pos = 0;

        public Color ReadyColor { get; set; }

        public Image LockImage { get; set; }

        public int SpeedUpdate
        {
            get
            {
                return timer.Interval;
            }
            set
            {
                timer.Interval = value;
            }
        }

        public LockButton()
        {
            ReadyColor = Color.Red;
            timer.Interval = 10;
            timer.Tick += timer_Tick;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (bState == ButtonState.Painting)
            {
                pos += 3;

                if (pos >= ClientRectangle.Width)
                {
                    bState = ButtonState.Ready;
                    timer.Stop();
                }
            }

            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            switch (bState)
            {
                case ButtonState.None:
                    pos = 0;
                    bState = ButtonState.Painting;
                    timer.Start();
                    break;
                case ButtonState.Painting:
                    break;
                case ButtonState.Ready:
                    base.OnMouseDown(mevent);
                    break;
            }
        }

        protected override void OnClick(EventArgs e)
        {
            if (bState == ButtonState.Ready)
            {
                bState = ButtonState.None;
                pos = 0;
                Invalidate();

                base.OnClick(e);
            }
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            Rectangle r = Rectangle.Empty;
            Brush b = null;

            switch (bState)
            {
                case ButtonState.Painting:
                case ButtonState.None:
                    r = new Rectangle(FlatAppearance.BorderSize, FlatAppearance.BorderSize, ClientSize.Width - 2 * FlatAppearance.BorderSize, ClientSize.Height - 2 * FlatAppearance.BorderSize);
                    b = new SolidBrush(Color.FromArgb(128, ReadyColor));

                    pevent.Graphics.FillRectangle(b, r);

                    r = new Rectangle(FlatAppearance.BorderSize + pos, FlatAppearance.BorderSize, ClientSize.Width - 2 * FlatAppearance.BorderSize - pos, ClientSize.Height - 2 * FlatAppearance.BorderSize);

                    if (LockImage != null)
                        b = new TextureBrush(LockImage);
                    else
                        b = new SolidBrush(Color.FromArgb(128, ReadyColor));
                    break;
                case ButtonState.Ready:
                    r = new Rectangle(FlatAppearance.BorderSize, FlatAppearance.BorderSize, ClientSize.Width - 2 * FlatAppearance.BorderSize, ClientSize.Height - 2 * FlatAppearance.BorderSize);
                    b = new SolidBrush(Color.FromArgb(128, ReadyColor));
                    break;
                default:
                    break;
            }

            if (b != null)
                pevent.Graphics.FillRectangle(b, r);
        }

        public void Reset()
        {
            timer.Stop();
            bState = ButtonState.None;
            pos = 0;
            Invalidate();
        }
    }
}
